package com.kakarote.ids.provider.controller;

import com.kakarote.common.result.BasePage;
import com.kakarote.common.result.Result;
import com.kakarote.common.result.ResultCode;
import com.kakarote.common.result.SystemCodeEnum;
import com.kakarote.ids.provider.entity.BO.DeptQueryBO;
import com.kakarote.ids.provider.entity.BO.UserQueryBO;
import com.kakarote.ids.provider.entity.VO.IdsDeptVO;
import com.kakarote.ids.provider.entity.VO.OrganizationVO;
import com.kakarote.ids.provider.entity.VO.UserVO;
import com.kakarote.ids.provider.service.AuthService;
import com.kakarote.ids.provider.service.DeptService;
import com.kakarote.ids.provider.service.UserService;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/adminUser"})
public class OAuthController {
    @Autowired
    private AuthService authService;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @PostMapping({"/authorization"})
    @ApiOperation("认证")
    public Result<String> authorization(@RequestParam("code") String code) {
        String authorization = authService.authorization(code);
        if (authorization != null)
            return Result.ok(authorization);
        return Result.error((ResultCode) SystemCodeEnum.TOKEN_VERIFICATION_EXCEPTION);
    }

    @ApiOperation("查询当前登录用户")
    @PostMapping({"/queryLoginUser"})
    public Result<UserVO> queryLoginUser() {
        return userService.queryLoginUser();
    }

    @PostMapping({"/queryOrganizationInfo"})
    @ApiOperation("查询完整组织架构信息")
    public Result<OrganizationVO> queryOrganizationInfo() {
        return userService.queryOrganizationInfo();
    }

    @PostMapping({"/queryUserList"})
    @ApiOperation("查询企业所有用户")
    public Result<BasePage<UserVO>> queryUserList(@RequestBody UserQueryBO userQueryBO) {
        return userService.queryUserList(userQueryBO);
    }

    @PostMapping({"/queryDeptTree"})
    @ApiOperation("查询部门列表树")
    public Result<List<IdsDeptVO>> queryDeptTree(@Validated @RequestBody DeptQueryBO queryBO) {
        return deptService.queryDeptTree(queryBO);
    }

    @PostMapping({"/logout"})
    @ApiOperation("退出登录")
    public Result<String> logout(@RequestHeader("AUTH-TOKEN") String token) {
        authService.logout(token);
        return Result.ok();
    }
}
